/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.bancofloripa.dao;

import br.com.bancofloripa.entity.Cliente;
import br.com.bancofloripa.entity.Agencia;
import br.com.bancofloripa.exception.MensagemExceptionDAO;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author fabianoc
 */
public class AgenciaDAO extends BaseDao{
    
   
   public int insere(Agencia cl) throws MensagemExceptionDAO {
       int id = 0;
        Connection c = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        int i = 0;
        try {
            StringBuilder sb = new StringBuilder();
            c = super.getConexaoBD();
            sb.append("INSERT INTO agencia ");
            sb.append("(nome) values (?)");
            ps = c.prepareStatement(sb.toString(), ps.RETURN_GENERATED_KEYS);
            ps.setString(++i, cl.getNome());
            ps.execute();
            rs = ps.getGeneratedKeys();
            rs.next();
            id = rs.getInt(1);
        } catch (SQLException ex) {
            throw new MensagemExceptionDAO("Erro de sql "+ex.getMessage());
        } catch (MensagemExceptionDAO ex) {
            ex.getMessage();
        } finally {
            this.closeConnection(ps, c);
        }       
        return id;
    }

    public void atualiza(Agencia ct) throws MensagemExceptionDAO {
        Connection c = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        StringBuilder sb = new StringBuilder();
        int i = 0;
        try {
            c = super.getConexaoBD();
            sb.append("UPDATE agencia SET ");
            sb.append("nome = ? WHERE id = ?");
            ps = c.prepareStatement(sb.toString());
            ps.setString(++i, ct.getNome());
            ps.setInt(++i, ct.getId());
            System.out.println("ID "+ct.getId());
            ps.execute();
        } catch (SQLException ex) {
            throw new MensagemExceptionDAO("Erro de sql "+ex.getMessage());
        } catch (MensagemExceptionDAO ex) {
            ex.getMessage();
        } finally {
            this.closeConnection(ps, c);
        }        
    }

    public List<Agencia> getAll() {

       List<Agencia> lista = new ArrayList<Agencia>();

       PreparedStatement ps = null;
       Connection c = null ;
       ResultSet rs = null;
       try {
            c = super.getConexaoBD();
            ps = c.prepareStatement("SELECT * FROM agencia");
            
            rs = ps.executeQuery();
            while ( rs.next() == true ) {
                Agencia cl = new Agencia( rs.getInt("id"), rs.getString("nome") );
                lista.add( cl );
            }
        } catch (MensagemExceptionDAO ex) {
            Logger.getLogger(AgenciaDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
         finally {
            this.closeConnection(ps, c);
       }
       return lista;
    }

    public Agencia getByFilter(int id) {
       Agencia cl = null;
       PreparedStatement ps = null;
       Connection c = null ;
       ResultSet rs = null;
       int i = 0;
       try {
            c = super.getConexaoBD();
            ps = c.prepareStatement("SELECT id, nome FROM agencia"
                    + " where id = ? "
                    );
            ps.setInt(++i, id);
            rs = ps.executeQuery();
            while ( rs.next() == true ) {
                
                cl = new Agencia( rs.getInt("id"), rs.getString("nome") );
            }
        } catch (MensagemExceptionDAO ex) {
            Logger.getLogger(AgenciaDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
         finally {
            this.closeConnection(ps, c);
       }
       return cl;
    }

 
            
    
}
